package com.christianposta.learnddd.domain.model.voyage;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.HashCodeBuilder;


import com.christianposta.learnddd.domain.shared.ValueObject;


public class Schedule implements ValueObject<Schedule> {
	
	private List<CarrierMovement> carrierMovements = Collections.EMPTY_LIST;

	// Null-object pattern
	public static final Schedule EMPTY = new Schedule();
	
	Schedule(final List<CarrierMovement> carrierMovements){
		Validate.notNull(carrierMovements, "Cannot create a schedule with null carrier movements");
		Validate.noNullElements(carrierMovements, "Cannot create a schedule with null elements in the carrier movements list");
		Validate.notEmpty(carrierMovements, "Cannot create a schedule with no elements in the carrier movements list");
		
		this.carrierMovements = carrierMovements;
	}
	
	public boolean sameValueAs(Schedule other) {
		return other != null && this.carrierMovements.equals(other.carrierMovements);
	}
	
	public List<CarrierMovement> carrierMovements(){
		return Collections.unmodifiableList(this.carrierMovements);
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) return true;
		if(obj == null || getClass() != obj.getClass()) return false;
		final Schedule that = (Schedule) obj;
		
		return sameValueAs(that);
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.carrierMovements).toHashCode();
	}

	Schedule() {
		// Needed by Hibernate
	}

}
